package com.kzw.oa.exam.web;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.kzw.core.bean.Msg;
import com.kzw.core.orm.Page;
import com.kzw.core.orm.PageRequest;
import com.kzw.core.orm.StringPropertyFilter;
import com.kzw.core.util.BeanUtil;
import com.kzw.core.util.ExportUtil;
import com.kzw.core.util.ExtjsUtil;
import com.kzw.core.util.StringUtil;
import com.kzw.core.util.json.JSON;
import com.kzw.core.util.json.JSON2;
import com.kzw.core.util.web.ResponseUtils;
import com.kzw.oa.exam.model.ExamResult;
import com.kzw.oa.exam.service.ExamResultService;
import com.kzw.oa.system.model.AppUser;
import com.kzw.oa.system.model.Department;

@Controller
@RequestMapping("/exam/examresult/")
public class ExamResultAction {

	@Autowired
	private ExamResultService examResultService;

	/**
	 * 首页
	 */
	@RequestMapping("index")
	public String index() {
		return "exam/examresult_view";
	}

	/**
	 * 显示列表
	 */
	@RequestMapping("list")
	public void list(HttpServletRequest request, HttpServletResponse response, Long orgId) {
		PageRequest pageRequest = ExtjsUtil.getPageReq(request);
		List<StringPropertyFilter> filters = ExtjsUtil.getFilters(request);
		if(orgId!=null && orgId!=0) {
			filters.add(new StringPropertyFilter("EQL_student.dept.depId", orgId+""));
		}
		
		Page<ExamResult> page = examResultService.search2(pageRequest, filters);

		String json = new JSON2(page).with(ExamResult.class, "oid", "student", "status", "score")
					.with(AppUser.class, "username", "fullname", "dept")
					.with(Department.class, "depName").toJson();
				
		ResponseUtils.renderJson(response, json);
	}

	/**
	 * 批量删除
	 */
	@ResponseBody
	@RequestMapping("multiDel")
	public Msg multiDel(String[] ids) {
		examResultService.remove(ids);
		return new Msg();
	}

	/**
	 * 显示详细信息
	 */
	@ResponseBody
	@RequestMapping("get")
	public Msg get(Long oid) {
		ExamResult examResult = examResultService.get(oid);
		String json = new JSON(examResult).buildWithFilters(2);
		return new Msg(true, 0, json);
	}

	/**
	 * 添加及保存操作
	 */
	@ResponseBody
	@RequestMapping("save")
	public Msg save(ExamResult examResult) {
		if (examResult.getOid() == null) {
			examResultService.saveOrUpdate(examResult);
		} else {
			ExamResult orgExamResult = examResultService.get(examResult.getOid());
			try {
				BeanUtil.copyNotNullProperties(orgExamResult, examResult);
				examResultService.saveOrUpdate(orgExamResult);
			} catch (Exception ex) {
				ex.printStackTrace();
			}
		}
		return new Msg();
	}
	
	/**
	 * iframe页面
	 */
	@RequestMapping("html/add")
	public String addUi() {
		return "exam/examresult_form";
	}
	
	@RequestMapping("html/edit")
	public String editUi(Long oid, Model model) {
		ExamResult examResult = examResultService.get(oid);
		model.addAttribute("examResult", examResult);
		return "exam/examresult_form";
	}
	
	/**
	 * 导出选中记录数
	 * */
	@RequestMapping("html/exportSel")
	public void exportSel(String ids, String colIds, String colNames, HttpServletResponse response) throws Exception {
		List<ExamResult> list = examResultService.getByIds(StringUtil.idStrToArr(ids, Long.class));
		
		String fname = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
		response.setContentType("application/x-msdownload");
		response.setHeader("content-disposition","attachment; filename="+fname+".xls");
		ExportUtil.ExportXls(list, response.getOutputStream(), colIds, colNames);
	}
	
	/**
	 * 导出全部记录
	 * */
	@RequestMapping("html/exportAll")
	public void exportAll(String colIds, String colNames, 
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		List<StringPropertyFilter> filters = ExtjsUtil.getFilters(request);
		List<ExamResult> list = examResultService.search(filters);
		
		String fname = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
		response.setContentType("application/x-msdownload");
		response.setHeader("content-disposition","attachment; filename="+fname+".xls");
		ExportUtil.ExportXls(list, response.getOutputStream(), colIds, colNames);
	}

}
